package com.sds.service.impl;

import com.sds.service.ThreadService;
import org.springframework.stereotype.Service;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * @Author: sds
 *
 * @Description: java多线程的实现方式
 */
@Service
public class ThreadServiceImpl implements ThreadService {

    @Override
    public void extendThread() {
        Thread threadTask = new Thread() {
            @Override
            public void run() {
                System.out.println("Thread task is running.");
            }
        };
        threadTask.start();
    }

    @Override
    public void implementRunnable() {
        Runnable runnableTask = () -> {
            System.out.println("Runnable task is running.");
        };
        new Thread(runnableTask).start();
    }

    @Override
    public Future<String> useCallable() {
        Callable<String> callableTask = () -> {
            System.out.println("Callable task is running.");
            return "Task completed";
        };
        FutureTask<String> futureTask = new FutureTask<>(callableTask);
        new Thread(futureTask).start();
        return futureTask;
    }
}